package com.zmn.oms.business.impl.attendance;

import com.alibaba.fastjson.JSON;
import com.zmn.base.engineer.common.dro.foreign.engineer.ForeignEngineerDetailInfoDRO;
import com.zmn.base.engineer.common.pagination.IPageInfo;
import com.zmn.base.engineer.common.query.foreign.engineer.ForeignEngineerQuery;
import com.zmn.base.engineer.dubbo.interfaces.foreign.EngineerListRemoteService;
import com.zmn.biz.engineer.common.dro.health.EngineerHealthDRO;
import com.zmn.biz.engineer.dubbo.interfaces.health.EngineerHealthListRemoteService;
import org.apache.dubbo.config.annotation.Reference;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.oms.business.interfaces.attendance.AttendanceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author: huangyifeng
 * @description: 健康考勤实现
 * @create: 2020-02-11 18:27
 **/
@Slf4j
@Service
public class AttendanceServiceImpl implements AttendanceService {

    @Reference(version = com.zmn.base.plat.engine.common.constant.DubboConsts.INTERFACE_VERSION , check = false)
    private EngineerHealthListRemoteService engineerHealthListRemoteService;

    @Reference(version = com.zmn.base.plat.engine.common.constant.DubboConsts.INTERFACE_VERSION , check = false)
    private EngineerListRemoteService engineerListRemoteService;



    @Override
    public void pushMasterHealthAttendance() {
        Date date = DateUtil.getNow();
        //todo findEngineerHealthByEngineerIdAndDay
        //ResponseDTO<EngineerHealthDRO> dto = engineerHealthListRemoteService.findEngineerHealthByEngineerIdAndDay(null, DateUtil.dateFormatToDate(date,"yyyy-MM-dd"));;
        // todo fubiao
        log.info("pushMasterHealthAttendance#getEngineerHealthByIdAndDay 入参 [{}]",1);
        ResponseDTO<EngineerHealthDRO> dto = engineerHealthListRemoteService.getEngineerHealthByIdAndDay(1, DateUtil.dateFormatToDate(date, "yyyy-MM-dd"));
        log.info("pushMasterHealthAttendance#getEngineerHealthByIdAndDay 出参",JSON.toJSONString(dto));
        // 下一页查询
        boolean next = true;

        // 页数
        int page = 1;
        while (next) {
            // todo listEngineerPushByPlatAndPage
            // engineerListRemoteService.listEngineerPushByPlatAndPage(GlobalConsts.PLAT_MARK_ZMN, page, 500);
            ForeignEngineerQuery foreignEngineerQuery = new ForeignEngineerQuery();
            foreignEngineerQuery.setPlat(com.zmn.consts.GlobalConsts.PLAT_MARK_ZMN);
            foreignEngineerQuery.setPageIndex(page);
            foreignEngineerQuery.setPageSize(500);
            // todo fubiao
            log.info("pushMasterHealthAttendance#listEngineerPageByQuery 入参 [{}]", JSON.toJSONString(foreignEngineerQuery));
            ResponseDTO<IPageInfo<ForeignEngineerDetailInfoDRO>> responseEngineerPush = engineerListRemoteService.listEngineerPageByQuery(foreignEngineerQuery);
            log.info("pushMasterHealthAttendance#listEngineerPageByQuery 出参 [{}]",JSON.toJSONString(responseEngineerPush));

            IPageInfo<ForeignEngineerDetailInfoDRO> engineerPushDRO = responseEngineerPush.getData();

            List<Integer> masterIdList = new ArrayList<>(engineerPushDRO.getTotal());
            List<ForeignEngineerDetailInfoDRO> foreignEngineerDetailInfoDROList = engineerPushDRO.getRecords();
            foreignEngineerDetailInfoDROList.stream().forEach(foreignEngineerDetailInfoDRO -> masterIdList.add(foreignEngineerDetailInfoDRO.getEngineerId()));

            if (masterIdList == null || masterIdList.size() <= 0) {
                next = false;
            } else {
                for (Integer masterId: masterIdList) {
                   // TODO
                   /* // 推送APP打卡消息
                    ResultDTO resultDTO = ClientPushUtil.sendInfo(String.valueOf(masterId),
                            "疫情蔓延勿轻视，为了您的健康，邀您填写每日健康卡。防疫攻坚，从我做起！",
                            "家修汇邀您填写每日健康卡", dto.getMessage(),
                            "100");*/
                  //  log.info("#ums#pushMasterHealthAttendance 推送健康打卡信息 app push result : {}",JSON.toJSONString(resultDTO));
                }
            }
            page ++;
        }
    }

    @Override
    public boolean pushMasterAttendance(Integer masterId) {
        Date date = DateUtil.getNow();
        // todo findEngineerHealthByEngineerIdAndDay
        // engineerHealthListRemoteService.findEngineerHealthByEngineerIdAndDay(masterId, DateUtil.dateFormatToDate(date,"yyyy-MM-dd"));
        ResponseDTO<EngineerHealthDRO> dto = null;
        // TODO
        EngineerHealthDRO engineerHealthDRO = dto.getData();
        // 推送APP打卡消息
       /* ResultDTO resultDTO = ClientPushUtil.sendInfo(String.valueOf(masterId),
                "疫情蔓延勿轻视，为了您的健康，邀您填写每日健康卡。防疫攻坚，从我做起！",
                "家修汇邀您填写每日健康卡", engineerHealthDRO.getUrl(),
                "100");
        log.info("#ums#pushMasterAttendance 推送师傅健康打卡信息 app push result : {}", JSON.toJSONString(resultDTO));
*/
        return false;//resultDTO.getStatusBool();
    }
}
